package com.app.web

import com.app.model.wrapper.Order
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.propertyeditors.CustomDateEditor
import org.springframework.web.bind.WebDataBinder
import org.springframework.web.bind.annotation.InitBinder

import javax.servlet.http.HttpServletRequest
import java.text.SimpleDateFormat

/**
 * Created by three on 14-8-11.
 */
@CompileStatic
@TypeChecked
class BaseAction {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, true));

        //String dateTime="yyyy-MM-dd HH:mm:ss";
        // binder.registerCustomEditor(Timestamp.class,new DatePropertyEditor(dateTime));
    }

    /**
     * 提供公共参数解析
     * <li>分页参数：pageNo:int</li>
     * <li>分页参数：iDisplayLength:int</li>
     * <li>排序参数：order:Order</li>
     *
     * @author  three
     * @date    2014年5月3日 下午8:45:46
     *
     * @param request
     * @return PageParams
     */
    protected CommonParams getCommonParams(HttpServletRequest request) {
        def iDisplayStart=request.getParameter("start")
        iDisplayStart = iDisplayStart==null? 1: iDisplayStart.isNumber()?iDisplayStart:1
        def iDisplayLength=request.getParameter("length")
        iDisplayLength = iDisplayLength==null? 10: iDisplayLength.isNumber()?iDisplayLength:10

        def orderCount = request.getParameter("order_count")
        orderCount = orderCount==null?0:orderCount.isNumber()?(orderCount as int):0;

        def orderList = new ArrayList<Order>();
        for(int i=0; i<orderCount; i++) {
            String orderBy = request.getParameter("order_"+i+"_column");
            orderBy = orderBy.collect{it == (it as String).toUpperCase() ? "_"+(it as String) :(it as String).toUpperCase()}.join("")
            String sortDir = request.getParameter("order_"+i+"_dir");

            Order order = null
            if (orderBy !=null && orderBy.size()>0){
                if (sortDir.toLowerCase() == "asc")
                    order = Order.asc(orderBy)
                else
                    order = Order.desc(orderBy)
            }

            orderList.add(order);
        }

        int pageNo= ((iDisplayStart as int)/(iDisplayLength as int) as int) + 1

        return  new CommonParams(orderList.size()>0?orderList.get(0):null, pageNo, iDisplayLength as int)
    }

    @CompileStatic
    @TypeChecked
    public class CommonParams{
        public CommonParams(Order order, int pageNo, int iDisplayLength) {
            this.order = order;
            this.pageNo = pageNo;
            this.iDisplayLength = iDisplayLength;
        }
        public Order order
        public int pageNo;
        public int iDisplayLength;

    }
}
